package org.zhss.demo.redis.geo;

import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;

/** 用户与商家的距离计算的案例 */
public class UserShopDistanceDemo {

  private Jedis jedis = new Jedis("192.168.150.201");

  /**
   * 添加一个地理位置
   *
   * @param name
   * @param longitude
   * @param latitude
   */
  public void addLocation(String name, double longitude, double latitude) {
    jedis.geoadd("location_data", longitude, latitude, name);
  }

  /**
   * 获取用户到商家的位置
   *
   * @param user
   * @param shop
   * @return
   */
  public double getDistance(String user, String shop) {
    return jedis.geodist("location_data", user, shop, GeoUnit.M);
  }

  public static void main(String[] args) {
    UserShopDistanceDemo demo = new UserShopDistanceDemo();
    demo.addLocation("张三", 116.49428833935545, 39.86700462665782);
    demo.addLocation("黑皮早点", 116.49424833935545, 39.86790462665782);
    System.out.println("用户到商家的距离为(米)：" + demo.getDistance("张三", "黑皮早点"));
  }
}
